﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    public class AccountManger : IAccountManger
    {
        /// <summary>
        /// 根据用户名登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByUserName(string userName, string password)
        {
            //判空
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                return OperResult<TBUsers>.Failed("用户名或者密码错误");
            }
            //通过用户名查询用户
            IAccountdService service = new AccountdService();
            TBLogins loginEntity = service.FindByUserName(userName);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("用户名不存在!!!");
            }
            //验证用户密码
            SHAEncryption sHAEncryption = new SHAEncryption();
            if (!sHAEncryption.SHA1Encrypt(password).Equals(loginEntity.Password))
            {
                return OperResult<TBUsers>.Failed("密码错误！！");
            }
            IUersServer uersServer = new UersServer();
            TBUsers porfileEntity = uersServer.FindByUserId(loginEntity.Id);
            return OperResult<TBUsers>.Succeed(porfileEntity);
        }
    }
}
